﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.IO;
using DTO;

namespace DAO
{
    public class KhachHangDAO
    {
        public static DataClasses1DataContext data = new DataClasses1DataContext();

        //Select * From
        public static List<KhachHang> LayDSKH()
        {
            try
            {
                var query = from khachhang in data.KhachHangs select khachhang;
                List<KhachHang> list = new List<KhachHang>();
                foreach (KhachHang kh in query)
                {
                    list.Add(kh);
                }
                return list;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //Insert into
        public static bool ThemKH(KhachHang kh)
        {
            try
            {
                data.KhachHangs.InsertOnSubmit(kh);
                data.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        //Update Where
        public static bool CapNhatKH(KhachHang m_kh)
        {
            try
            {
                KhachHang kh = data.KhachHangs.Single(khachhang => khachhang.ID == m_kh.ID);
                kh.HoTen = m_kh.HoTen;
                kh.DiaChi = m_kh.DiaChi;
                kh.DienThoai = m_kh.DienThoai;
                kh.CMND = m_kh.CMND;
                data.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        //Delete From Where
        public static bool XoaKH(int ma)
        {
            try
            {
                KhachHang kh = data.KhachHangs.Single(khachhang => khachhang.ID == ma);
                data.KhachHangs.DeleteOnSubmit(kh);
                data.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        //Search
        public static List<KhachHang> TimKiemMaKH(string stringSearch)
        {
            try
            {
                var query = from khachhang in data.KhachHangs where khachhang.ID.ToString().Equals(stringSearch) select khachhang;
                List<KhachHang> ResultSearch = new List<KhachHang>();
                foreach (KhachHang kh in query)
                {
                    ResultSearch.Add(kh);
                }
                return ResultSearch;
            }
            catch
            {
                return null;
            }
        }

        public static List<KhachHang> TimKiemHoTen(string stringSearch)
        {
            try
            {
                var query = from khachhang in data.KhachHangs select khachhang;
                List<KhachHang> ResultSearch = new List<KhachHang>();
                foreach (KhachHang kh in query)
                {
                    if (kh.HoTen.Contains(stringSearch) || kh.HoTen.ToLower().Contains(stringSearch))
                        ResultSearch.Add(kh);
                }
                return ResultSearch;
            }
            catch
            {
                return null;
            }
        }

        public static List<KhachHang> TimKiemDiaChi(string stringSearch)
        {
            try
            {
                var query = from khachhang in data.KhachHangs select khachhang;
                List<KhachHang> ResultSearch = new List<KhachHang>();
                foreach (KhachHang kh in query)
                {
                    if (kh.DiaChi.Contains(stringSearch) || kh.DiaChi.ToLower().Contains(stringSearch))
                        ResultSearch.Add(kh);
                }
                return ResultSearch;
            }
            catch
            {
                return null;
            }
        }

        public static List<KhachHang> TimKiemDienThoai(string stringSearch)
        {
            try
            {
                var query = from khachhang in data.KhachHangs select khachhang;
                List<KhachHang> ResultSearch = new List<KhachHang>();
                foreach (KhachHang kh in query)
                {
                    if (kh.DienThoai == stringSearch)
                        ResultSearch.Add(kh);
                }
                return ResultSearch;
            }
            catch
            {
                return null;
            }
        }
    }
}
